import 'dart:convert';

import 'package:common_utils/common_utils.dart';
import 'package:dio/dio.dart';
import 'package:flutter_chubao/tools/app_tool_toast.dart';

import 'package:flutter_chubao/tools/tool_app/app_dio_response.dart';

import 'package:get/get.dart' as Getx;

import 'app_dio_transformer.dart';

class AppDefaulTransformer extends AppDioTransformer {
  @override
  AppDioResponse parse(Response response) {
    if (response.data is String) {
      response.data = jsonDecode(response.data);
    }
    LogUtil.d(jsonEncode(response.data));
    if (response.data["code"] == 200) {
      return AppDioResponse.success(response.data["data"]);
    } else {
      // if (response.data['code'] == 401) {
      //   JFmanagerUser.instance.userLoginOut();
      //   Getx.Get.toNamed(JFRouteNames.JfLoginMianPage);
      // } else if (response.data['code'] == 10002) {
      //   // HttpClient.get('/init/server_time').then((response) {
      //   //   if (response.ok) {
      //   //     int servericetime = response.data;
      //   //     HttpClient.timeDifference =
      //   //         DateTime.now().millisecondsSinceEpoch - servericetime;
      //   //     DLog(HttpClient.timeDifference);
      //   //   }
      //   // });
      // } else {
      AppToolToast.showToast(response.data['msg'] ?? '');
      // }

      return AppDioResponse.failure(
          errorMsg: response.data["msg"], errorCode: response.data["code"]);
    }
  }

  /// 单例对象
  static final AppDefaulTransformer _instance =
      AppDefaulTransformer._internal();

  /// 内部构造方法，可避免外部暴露构造函数，进行实例化
  AppDefaulTransformer._internal();

  /// 工厂构造方法，这里使用命名构造函数方式进行声明
  factory AppDefaulTransformer.getInstance() => _instance;
}
